﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MensUnoShop.Models;
using MensUnoShop.Filters;
using MensUnoShop.Models.ViewModels;
using System.Xml.Linq;

namespace MensUnoShop.Controllers
{
    public class RegionController : Controller
    {
        //
        // GET: /Region/
        private string[] _view = new string[] { "", "mainDepartment", "subDepartment", "n"};
        private Member _member;
        private List<Tag> _toptaglist,_navlist,_leftlist;
        private string _title;
        private AD _ad;
        private List<UNews> _newslist;
        private XElement _hotkeylist;

        public RegionController()
        {
            _member = MemberFactory.GetMember();
            _toptaglist = TagFactory.GetTopTagList();
            _ad = new AD();
            _newslist = NewsFactory.GetNewsList();
            _hotkeylist = KeyWordFactory.GetHotKeyList();
        }
        
        public ActionResult Index()
        {
            IndexViewModel iv = new IndexViewModel { MEMBER = _member, TITLE = _title, TOPLIST = _toptaglist, AD=_ad,NEWS=_newslist,HOTKEYLIST=_hotkeylist };
            return View(iv);
        }

        [RegionFilter]
        public ActionResult Product(string c_1,string c_2, string mguid)
        {
            
            if (ViewData["tag_c2"] != null)
            {
                //次館別
                Tag c2 = (Tag)ViewData["tag_c2"];
                _navlist = (List<Tag>)ViewData["navlist"];
                _leftlist = TagFactory.GetLeftTagList((int)c2.PARENTID);
                List<SimpleMarket> marketlist = MarketFactory.GetMarketListOpen(c2.ID);
                RegionViewModel iv = new RegionViewModel { MEMBER = _member, TITLE = _title, TOPLIST = _toptaglist, NAVLIST = _navlist, LEFTTAG = _leftlist, MARKETLIST = marketlist, AD = _ad, NEWS = _newslist, HOTKEYLIST = _hotkeylist };
                return View(_view[2], iv);
            }
            else if (ViewData["tag_c1"] != null)
            {
                //館別
                Tag c1 = (Tag)ViewData["tag_c1"];
                _navlist = (List<Tag>)ViewData["navlist"];
                _leftlist = TagFactory.GetLeftTagList((int)c1.ID);
                List<SimpleMarket> marketlist = MarketFactory.GetMarketListOpen(c1.ID,8);
                RegionViewModel iv = new RegionViewModel { MEMBER = _member, TITLE = _title, TOPLIST = _toptaglist, NAVLIST = _navlist, LEFTTAG = _leftlist, MARKETLIST = marketlist, AD = _ad, NEWS = _newslist, HOTKEYLIST = _hotkeylist };
                return View(_view[1], iv);
            }
            else
                return RedirectToAction("Index");
        }

        public ActionResult Search(string keyword)
        {
            string _keyword = keyword ?? string.Empty;
            List<SimpleMarket> resultlist = null;
            MarketSearch ms = null;
            _navlist = new List<Tag>();
            _navlist.Add(new Tag() { TITLE = "搜尋結果: "+keyword });
            _leftlist = TagFactory.GetLeftTagList(1);
            if (string.IsNullOrEmpty(_keyword))
            {
                resultlist = new List<SimpleMarket>();
            }
            else
            {
                
                ms = new MarketSearch(_keyword);
                //取得搜尋結果
                resultlist = ms.GetResult();
                
            }

            RegionViewModel iv = new RegionViewModel { MEMBER = _member, TITLE = _title, TOPLIST = _toptaglist, NAVLIST = _navlist, LEFTTAG = _leftlist, MARKETLIST = resultlist, AD = _ad, NEWS = _newslist, HOTKEYLIST = _hotkeylist };

            return View(_view[2], iv);
        }

        [RegionFreeFilter]
        public ActionResult FreeTag(string c_1, string c_2)
        {
            Tag c1 = (Tag)ViewData["tag_c1"];
            Tag c2 = (Tag)ViewData["tag_c2"];
            _navlist = (List<Tag>)ViewData["navlist"];
            _leftlist = TagFactory.GetLeftTagList((int)c1.ID);
            List<SimpleMarket> marketlist = MarketFactory.GetMarketListFree(c1.ID,c2.ID);
            RegionViewModel iv = new RegionViewModel { MEMBER = _member, TITLE = _title, TOPLIST = _toptaglist, NAVLIST = _navlist, LEFTTAG = _leftlist, MARKETLIST = marketlist, AD = _ad, NEWS = _newslist, HOTKEYLIST = _hotkeylist };
            return View(_view[2], iv);
        }

        [RegionFreeFilter]
        public ActionResult ComboTag(string c_1, string c_2)
        {
            Tag c1 = (Tag)ViewData["tag_c1"];
            Tag c2 = (Tag)ViewData["tag_c2"];
            _navlist = (List<Tag>)ViewData["navlist"];
            _leftlist = TagFactory.GetLeftTagList((int)c1.ID);
            List<SimpleMarket> marketlist = MarketFactory.GetMarketListCombo(c1.ID, c2.ID);
            List<TinyMarket> combomarketlist = new List<TinyMarket>();
            ViewData["combomarketlist"] = combomarketlist;
            RegionViewModel iv = new RegionViewModel { MEMBER = _member, TITLE = _title, TOPLIST = _toptaglist, NAVLIST = _navlist, LEFTTAG = _leftlist, MARKETLIST = marketlist, AD = _ad, NEWS = _newslist, HOTKEYLIST = _hotkeylist };
            return View(iv);
        }

        

    }
}
